#ifndef _CRITICALSECTION_H_
#define _CRITICALSECTION_H_

#include<pthread.h>

class CriticalSection  
{
public:
	CriticalSection() throw() {
		pthread_mutex_init(&_mutex, NULL);
	}
	~CriticalSection() throw() {
		pthread_mutex_destroy(&_mutex);
	}
	void enter() throw() {
		pthread_mutex_lock(&_mutex);
	}
	void leave() throw() {
		pthread_mutex_unlock(&_mutex);
	}
	pthread_mutex_t& getMutex() {
		return _mutex;
	}

private:
	pthread_mutex_t _mutex;

	CriticalSection(const CriticalSection&);
	CriticalSection& operator=(const CriticalSection&);
};


class Lock {
public:
	Lock(CriticalSection& cs) throw() : _cs(cs) { _cs.enter(); }
	~Lock() throw() { _cs.leave(); }
private:
	Lock& operator=(const Lock&);
	CriticalSection& _cs;
};

#endif // _CRITICALSECTION_H_

